<template>
  <el-dialog append-to-body class="common_dialog_in" custom-class="reset-dialog-width" :visible.sync="visible" title="选择配送柜" :close-on-click-modal="false" :close-on-press-escape="false">
    <CaninetList v-if="visible" ref="CaninetListItem" :searchItem="true" :listBorder="true" :qPNotInGroupNo="cabinetGroupNo" :notInRegionId="regionId" :chooseTable="true"></CaninetList>
    <template slot="footer">
      <el-button :loading="confirmLoading" type="primary" @click="formSubmitHandle()">确 定</el-button>
      <el-button @click="visible = false">取 消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
import CaninetList from './caninetList.vue'  //配送柜列表组件
import { batchUpdateGroup,batchUpdateRegion } from "@/api/deliver/cabinetGroup";
export default {
  data() {
    return {
      visible: false,
      confirmLoading: false,
      cabinetGroupNo: '',
      regionId: '',
    }
  },
  components: {
    CaninetList, //配送柜列表组件
  },
  methods: {
    init() {
      this.visible = true
      this.$nextTick(() => {
        this.confirmLoading = false
      })
    },
    // 表单提交
    formSubmitHandle: debounce(function () {

      console.log(this.regionId)
      console.log(this.cabinetGroupNo)

      this.confirmLoading = true
      let valList = []
      let value = this.$refs.CaninetListItem.value
      value.map((item) => {
        valList.push(item.seriesNo)
      })
      const data = {
          seriesNos: valList,
          cabinetGroupNo: this.cabinetGroupNo,
          regionId: this.regionId,
      }
      if (this.regionId) {
        batchUpdateRegion(data).then((res) => {
          this.$modal.msgSuccess("修改成功");
          this.visible = false
          this.confirmLoading = false
          this.$emit('refreshDataList')
        }).catch(() => {
          this.confirmLoading = false
        });
      } else if (this.cabinetGroupNo) {
        batchUpdateGroup(data).then((res) => {
          this.$modal.msgSuccess("修改成功");
          this.visible = false
          this.confirmLoading = false
          this.$emit('refreshDataList')
        }).catch(() => {
          this.confirmLoading = false
        });
      }
    }, 1000, { 'leading': true, 'trailing': false }),

  }
}
</script>
<style lang="scss" scoped>
.common_dialog_in {
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  ::v-deep.el-dialog {
    margin: 0 auto !important;
    max-height: 1000px;

    .el-dialog__body {
      overflow: hidden;
      overflow-y: auto;
      max-height: 800px;
    }
  }
}
</style>
